package util;

import sniff.pack.MyARPPacket;
import sniff.pack.MyICMPPacket;
import sniff.pack.MyOSPFPacket;
import sniff.pack.MyTCPPacket;
import sniff.pack.MyUDPPacket;

public class InfoUtil {
	public static String getTCPInfo(MyTCPPacket tcpPack) {
		StringBuffer sb = new StringBuffer();
		sb.append(tcpPack.getSourctPort() + "->" + tcpPack.getDestinationPort() + " [");
		if (tcpPack.getUrg() == 1) {
			sb.append("URG,");
		}
		if (tcpPack.getAck() == 1) {
			sb.append("ack,");
		}
		if (tcpPack.getPsh() == 1) {
			sb.append("PSH,");
		}
		if (tcpPack.getRst() == 1) {
			sb.append("RST,");
		}
		if (tcpPack.getSyn() == 1) {
			sb.append("SYN,");
		}
		if (tcpPack.getFin() == 1) {
			sb.append("FIN,");
		}
		sb.substring(0, sb.length() - 1);
		sb.append("] Seq=" + tcpPack.getSeqNum());
		if (tcpPack.getAck() == 1) {
			sb.append(" ACK=" + tcpPack.getAck());
		}
		sb.append(" Win=" + tcpPack.getWindowSize());
//		sb.append(" Len=" + tcpPack.getData().length);

		return sb.toString();
	}

	public static String getARPInfo(MyARPPacket myArpPack) {
		String info = "";
		switch (myArpPack.getOperation()) {
		case "request(1)":
			info = "who has " + myArpPack.getTargetIP() + "? Tell " + myArpPack.getSenderIP();
			break;
		case "reply(2)":
			info = myArpPack.getSenderIP() + " is at " + myArpPack.getSenderMac();
			break;
		default:
			info = "other";
			break;
		}
		return info;
	}

	public static String getUDPInfo(MyUDPPacket udpPack) {
		String info = "";
		info += udpPack.getSourctPort() + " -> " + udpPack.getDestinationPort() + " Len=" + udpPack.getUdpLen();
		return info;
	}

	public static String getICMPInfo(MyICMPPacket icmpPack) {
		String info = "";
		switch (icmpPack.getIcmpType()) {
		case 0:
			info += "(Echo (ping) reply) ";
			break;
		case 8:
			info += "(Echo (ping) request) ";
			break;
		default:
			break;
		}
		info+="id="+icmpPack.getIdentifierBE()+", Seq="+icmpPack.getSeqNumBE()+"/"+icmpPack.getSeqNumLE();
		return info;
	}
	
	public static String getOSPFInfo(MyOSPFPacket ospfPack) {
		String info="";
		switch(ospfPack.getPackType()) {
		case 1:
			info+="(Hello)";
			break;
		case 2:
			info+="(Database Description)";
			break;
		case 3:
			info+="(Link State Request)";
			break;
		case 4:
			info+="(Link State Update)";
			break;
		case 5:
			info+="(Link State Acknowledgment)";
			break;
		default:
			break;
		}
		return info;
	}
}
